package com.recyclerview426lkx.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.recyclerview426lkx.R;
import com.recyclerview426lkx.activity.ArticleDetailActivity;
import com.recyclerview426lkx.adapter.ArticleAdapter;
import com.recyclerview426lkx.bean.ArticleBean;
import com.recyclerview426lkx.iface.ArticalListener;
import com.recyclerview426lkx.model.ArticleModel;

import java.util.List;

/**
 * Created by acer on 2017/5/23.
 */

public class ArticleFragment extends Fragment {
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private ArticleAdapter articleAdapter;
    private List<ArticleBean> list=null;
    private View view=null;
    private Context context;
    private static int cur_page=1;
   private static int lastVisibleItemPosition;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view=inflater.inflate(R.layout.article,container,false);
        context=view.getContext();

        initRecyclerView();
        ArticleModel articleModel =new ArticleModel();

        articleModel.getReultList("article",2,"64qqm32ivttc79bvvst1v7abs0",articleListener);

        return view;
    }
    ArticalListener articleListener =new ArticalListener() {
        @Override
        public void onResponse(List<ArticleBean> listbean) {

  //          list=listbean;
   //         myAdapter.setList(list);

         if(cur_page==1){
              list=listbean;
             articleAdapter.setList(list);

         }else{
             list.removeAll(listbean);

             list.addAll(0,listbean);
             articleAdapter.setList(list);
          }

        }

        @Override
        public void onFaile(String msg) {
            Toast.makeText(context,"fail:"+msg,Toast.LENGTH_SHORT).show();
        }
    };
    private void initRecyclerView() {
        recyclerView=(RecyclerView)view.findViewById(R.id.rv_article);

        //创建默认的线性LayoutManager
        layoutManager =new LinearLayoutManager(context);
        //设置布局
        recyclerView.setLayoutManager(layoutManager);
        //设置item的高度
        recyclerView.setHasFixedSize(true);
        //创建适配器
//        list=getData();
        articleAdapter=new ArticleAdapter(context,list);
        ArticleAdapter.OnItemClickListener onItemClickListener=new ArticleAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent=new Intent(getActivity(), ArticleDetailActivity.class);
                intent.putExtra("resid",list.get(position).getId());
                startActivity(intent);
            }
        };

        articleAdapter.setmOnItemClickListener(onItemClickListener);
        articleAdapter.setList(list);

        recyclerView.setAdapter(articleAdapter);
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
          @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {

                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == list.size()) {
                    cur_page+=1;

                    new ArticleModel().getReultList("article",cur_page,"8soafn3tpaj0efuab5jvmpbhr4",articleListener);
                }
            }

            @Override
           public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItemPosition=list.size()-1;
            }
        });
    }

}
